package com.itheima.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.utils.MapperUtils;
import java.util.List;

public class StudentServiceImpl implements StudentService {
    @Override
    public List<Student> findAll() {
        try{
            StudentMapper studentMapper = MapperUtils.getMapper(StudentMapper.class);
            List<Student> list = studentMapper.findAll();
            return list;
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            MapperUtils.close();
        }
        return null;
    }

  /*分页查询
    * 导入两个jar包
    * 配置核心配置文件
    */
    @Override
    public PageInfo<Student> findByPage(int currentPage,int pageSize) {
        try{
            StudentMapper studentMapper = MapperUtils.getMapper(StudentMapper.class);
            //查询数据之前设置分页参数
            PageHelper.startPage(currentPage,pageSize);
            List<Student> list = studentMapper.findAll();
            //***查询完之后，要封装一个PageInfo对象，里面包含页面展示分页条需要的信息
            PageInfo<Student> pageInfo = new PageInfo<>(list);
            return pageInfo;
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public int addStu(Student stu) {
    try{
        StudentMapper studentMapper = MapperUtils.getMapper(StudentMapper.class);
        int i = studentMapper.addStu(stu);
        MapperUtils.commit();
        return i;
    }catch(Exception e){
        e.printStackTrace();
        MapperUtils.rollback();
    }finally{
        MapperUtils.close();
    }
        return -1;
    }


    @Override
    public int updateStu(Student stu) {
        try{
            StudentMapper studentMapper = MapperUtils.getMapper(StudentMapper.class);
            int result = studentMapper.updateStu(stu);
            MapperUtils.commit();
            return result;
        }catch(Exception e){
            e.printStackTrace();
            MapperUtils.rollback();
        }finally{
            MapperUtils.close();
        }
        return -1;
    }



    @Override
    public int deleteStu(Integer id) {
        try{
            StudentMapper studentMapper = MapperUtils.getMapper(StudentMapper.class);
            int result = studentMapper.deleteStu(id);
            MapperUtils.commit();
            return result;
        }catch(Exception e){
            e.printStackTrace();
            MapperUtils.rollback();
        }finally{
            MapperUtils.close();
        }
        return -1;
    }
/*动态sql查询学生信息*/
    @Override
    public List<Student> findByCondition(Student stu) {
        try {
            StudentMapper studentMapper = MapperUtils.getMapper(StudentMapper.class);
            List<Student> list = studentMapper.findByCondition(stu);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }
/*动态sql通过多id查询学生信息*/
    @Override
    public List<Student> findByIds(Integer... ids) {
        try{
            StudentMapper studentMapper = MapperUtils.getMapper(StudentMapper.class);
            List<Student> students = studentMapper.findByIds(ids);
            return students;
        }catch ( Exception e){
            e.printStackTrace();
        }finally{
            MapperUtils.close();
        }
        return null;
    }
/*动态sql修改学生信息*/
    @Override
    public int updateStu2(Student stu) {
        try{
            StudentMapper studentMapper = MapperUtils.getMapper(StudentMapper.class);
            int result = studentMapper.updateStu2(stu);
            MapperUtils.commit();
            System.out.println(result);
            return result;
        }catch(Exception e){
            e.printStackTrace();
            MapperUtils.rollback();
        }finally{
            MapperUtils.close();
        }

        return -1;
    }

/**动态sql增加学生信息
**/
    @Override
    public int addStu2(Student stu) {
        return 0;
    }

/** 动态sql删除学生信息
**/
    @Override
    public int deleteStu2(Integer id) {
        return 0;
    }
}
